package com.zhixue.content.util;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @Auther: 李 力
 * @Date: 2025/5/12
 * @Description: com.zhixue.content.util
 * @version: 1.0
 */
@Data
@Slf4j
public class SecurityUtil {
    public static CurrentUser getUser() {
        try {
            Object principalObj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (principalObj instanceof String) {
                //取出用户身份信息
                String principal = principalObj.toString();
                //将json转成对象
                CurrentUser user = JSON.parseObject(principal, CurrentUser.class);
                return user;
            }
        } catch (Exception e) {
            log.error("获取当前登录用户身份出错:{}", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
}